﻿using System.Web.Security;
using RLAWEB.Models;
using RLAWEB.RLAServiceReference;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace RLAWEB
{
    public partial class Achievement : Page
    {
        private IRealLifeService _rlaServiceClient;
        private UserAchievement _ua;

        protected void Page_Load(object sender, EventArgs e)
        {
            _rlaServiceClient = new RealLifeServiceClient();
            lbPageTitle.Text = "";
            var authCookie = Request.Cookies[FormsAuthentication.FormsCookieName];
            if (authCookie != null)
            {
                User loggedInUser = Helper.GetLoggedInUser(authCookie);
                int achId;
                // if (!IsPostBack)
                {
                    if (int.TryParse(Request.QueryString["ID"], out achId))
                    {
                        _ua = _rlaServiceClient.GetUserAchievement(loggedInUser.UserId, achId);
                        if (_ua != null)
                        {
                            lbPageTitle.Text = _ua.Achievement.Title;
                            SetAchievementInfo();
                            var participants = _ua.Achievement.Participants;
                            if (participants != null)
                            {
                                ParticipantList.DataSource = participants;
                                ParticipantList.DataBind();
                            }
                            else
                            {
                                lbParticipantHeader.Text = "Fejl under indlæsning af deltagerliste";
                            }
                        }
                    }
                }
            }
        }

        private void SetAchievementInfo()
        {
            btnLeft.Visible = true;
            btnCenter.Visible = true;
            btnRight.Visible = true;

            lbDescription.Text = _ua.Achievement.Description;
            lbCategory.Text = _ua.Achievement.Category;
            lbTags.Text = _ua.Achievement.Tag;

            switch (_ua.Achievement.CurrentStatus)
            {
                case RLAServiceReference.Achievement.Status.Active:
                    lbStatus.Text = "aktiv";
                    break;

                case RLAServiceReference.Achievement.Status.InActive:
                    lbStatus.Text = "inaktiv";
                    break;
            }

            switch (_ua.CurrentStatus)
            {
                case UserAchievement.Status.Done:
                    lblMyStatus.Text = "Gennemført";
                    btnRight.Text = "Opgiv";
                    btnCenter.Visible = false;
                    break;

                case UserAchievement.Status.Failed:
                    lblMyStatus.Text = "Fejlet";
                    btnCenter.Visible = false;
                    btnRight.Visible = false;
                    break;

                case UserAchievement.Status.InProgress:
                    lblMyStatus.Text = "Igangværende";
                    btnCenter.Text = "Opgiv";
                    btnRight.Text = "Fuldført";
                    break;

                case UserAchievement.Status.Invited:
                    lblMyStatus.Text = "Inviteret";
                    btnCenter.Text = "Afslå";
                    btnRight.Text = "Acceptér";
                    break;

                case UserAchievement.Status.Rejected:
                    lblMyStatus.Text = "Afvist";
                    btnCenter.Visible = false;
                    btnRight.Visible = false;
                    break;
            }
        }

        protected void btnRight_Click(object sender, EventArgs e)
        {
            if (_ua != null)
            {
                int errorCode = 0;
                switch (_ua.CurrentStatus)
                {
                    case UserAchievement.Status.InProgress:
                        _ua.CurrentStatus = UserAchievement.Status.Done;
                        errorCode = _rlaServiceClient.SetUserAchievementStatus(_ua);
                        break;

                    case UserAchievement.Status.Invited:
                        _ua.CurrentStatus = UserAchievement.Status.InProgress;
                        errorCode = _rlaServiceClient.SetUserAchievementStatus(_ua);
                        break;

                    case UserAchievement.Status.Done:
                        _ua.CurrentStatus = UserAchievement.Status.Rejected;
                        errorCode = _rlaServiceClient.SetUserAchievementStatus(_ua);
                        break;

                    case UserAchievement.Status.Failed:
                        //Should not be able to do anything, button should be hidden
                        break;

                    case UserAchievement.Status.Rejected:
                        //Should not be able to do anything, button should be hidden
                        break;
                }
                Response.Redirect(Request.RawUrl);
            }
        }

        protected void btnLeft_Click(object sender, EventArgs e)
        {
            Response.Redirect("ListAchievements.aspx");
        }
    }
}